#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` in.vcf
exit 65
}

while getopts  ":b:p:" opts
do
	case  $opts  in
	b) build=$OPTARG;;
	p) out_prefix=$OPTARG;;
	*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi


. /mnt/ilustre/app/medical/tools/.var

if test -z "$build"; then build=hg19; fi

# annovar_path=$tools_path/annovar_path_z
# annovar_db_path=$annovar_path/humandb/

annovar_path=$tools_path/annovar
annovar_db_path=$annovar_path/$build

# /file_path/run_annovar.pl
# /file_path/SN.filtered.cluster_filtered.passcode.SNP.vcf

# $annovar_path/table_annovar.pl $1 $annovar_db_path -vcfinput -buildver $build -out $out_prefix -remove -protocol refGene,cytoBand,genomicSuperDups,esp6500siv2_all,1000g2014oct_all,1000g2014oct_afr,1000g2014oct_eas,1000g2014oct_eur,snp138,ljb26_all -operation g,r,r,f,f,f,f,f,f,f -nastring . -csvout

$annovar_path/table_annovar.pl $1 $annovar_db_path -vcfinput -buildver $build -out $out_prefix -remove -protocol refGene,cytoBand,genomicSuperDups,clinvar_20150629,cosmic70,ljb23_pp2hvar,ljb23_pp2hdiv,ljb23_sift,ljb23_mt -operation g,r,r,f,f,f,f,f,f -nastring . --thread 6

# WARNING to old ANNOVAR users: this program no longer does line-to-line conversion for multi-sample VCF files. If you want to include all variants in output, use '-format vcf4old' or use '-format vcf4 -allsample -withfreq' instead.

# no need to filter because will do it using gatk
# $annovar_path/convert2annovar.pl --format vcf4 --includeinfo --coverage 20 --fraction 0.05 --outfile $out_prefix.avi.impact.txt $1

:<<date0531
$annovar_path/convert2annovar.pl --format vcf4 --includeinfo --outfile $out_prefix.avi.impact.txt $1

$annovar_path/table_annovar.pl $out_prefix.avi.impact.txt $annovar_db_path -buildver $build -out $out_prefix.impact -remove -protocol knowngene,refGene,cosmic70,ljb26_all,esp6500si_all,snp138,1000g2014oct_all  -operation g,g,f,f,f,f,f --thread 6
date0531


# Error in argument: -csvout is not compatible with -vcfinput
# Error in argument: -nastring must be '.' when '-vcfinput' is specified

. $cmd_done
exit 0

# convert variant VCF4 file to ANNOVAR format
echo; echo convert variant VCF4 file to ANNOVAR format
$annovar_path/convert2annovar.pl --format vcf4old $1 -outfile $out_prefix.avi.txt

# Annotate with human genes
echo; echo Annotate with human genes
$annovar_path/annotate_variation.pl -geneanno -buildver $build $out_prefix.avi.txt $annovar_db_path

# Determine whether a mutation intersects with a known cancer gene. We use the Cancer Gene Census gene lists and BEDTools to determine whether the mutation occurs in a known cancer gene

# Create a mutation bed file for BEDTools.
echo; echo avi to bed
cut -f1-3,5 $out_prefix.avi.txt > $out_prefix.avi.bed


# Intersect the mutations with cancer gene coordinates
echo; echo intersect bed with cancer gene coordinates
intersectBed -a $out_prefix.avi.bed -b $data_path/intervals/1/bt_clean.bed -wa -wb > $out_prefix.intersect.txt


# Annotate with ClinVar database.
echo; echo anno clinvar
$annovar_path/annotate_variation.pl -filter -dbtype clinvar_20150629 -buildver $build $out_prefix.avi.txt $annovar_db_path

# Annotate with COSMIC database.
echo; echo anno cosmic
$annovar_path/annotate_variation.pl -filter -dbtype cosmic70 -buildver $build $out_prefix.avi.txt $annovar_db_path

# Annotate with PolyPhen score database.
echo; echo anno pph
$annovar_path/annotate_variation.pl -filter -dbtype ljb23_pp2hvar -buildver $build $out_prefix.avi.txt $annovar_db_path

echo; echo anno pph
$annovar_path/annotate_variation.pl -filter -dbtype ljb23_pp2hdiv -buildver $build $out_prefix.avi.txt $annovar_db_path

# Annotate with SIFT score database.
echo; echo anno sift
$annovar_path/annotate_variation.pl -filter -dbtype ljb23_sift -buildver $build $out_prefix.avi.txt $annovar_db_path


# Annotate with Mutation Taster database.
echo; echo anno mutation taster
$annovar_path/annotate_variation.pl -filter -dbtype ljb23_mt -buildver $build $out_prefix.avi.txt $annovar_db_path


# run_anovar.pl